package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.ItemArgument
import com.gin.kotlin.no_gay_alliance.model.ItemDataModel
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.SingleData
import com.gin.nga.response.item.ItemData
import java.lang.ref.WeakReference

/**
 * 道具仓库提供者
 * @property dir String
 */
class ItemInventoryProvider private constructor():AbstractCachedDataProvider<ItemArgument, SingleData<ItemData>,ItemDataModel>(
    javaType = TYPE_FACTORY.constructType(ItemDataModel::class.java)
) {
    override val dir="ITEM_INVENTORY"

    override fun transform(rawData: SingleData<ItemData>) = rawData.data?.let { ItemDataModel(it) } ?: ItemDataModel()

    override fun requestMethod(ngaApi: NgaApi, params: ItemArgument?) = ngaApi.itemApi.inventory(params?.page)

    override fun obtainRelativePath(userId: Long, params: ItemArgument?) = "${userId}_${params?.page?:1}.json"

    companion object {
        private var instance: WeakReference<ItemInventoryProvider> = WeakReference(ItemInventoryProvider())
        fun getInstance(): ItemInventoryProvider = instance.get() ?: ItemInventoryProvider().also { instance = WeakReference(it) }
    }
}